import com.sun.jdi.Value;

import java.util.*;

public class J102501 {
    public static List<Integer> findSubstring(String s, String[] words) {
        Set<Integer> ret = new HashSet<>();
        Map<String, Integer> hashw = new HashMap<>();
        int len = words[0].length();
        int slen = s.length();
        int wlen = words.length;

        //words入哈希
        for (String str : words) {
            hashw.put(str, hashw.getOrDefault(str, 0) + 1);
        }


        for (int i = 0; i < len; i++) {//i为不同组合的起点位置
            Map<String, Integer> hashs = new HashMap<>();
            int count = 0;//记录有效字符串个数

            //以下大体思路同“字符异位子串”，需要注意的是left和right每次移动的步数
            for (int left = i, right = i; right <= slen - len; right += len) {
                String str1 = s.substring(right, right + len);
                hashs.put(str1, hashs.getOrDefault(str1, 0) + 1);
                if (hashs.get(str1) <= hashw.getOrDefault(str1, 0)) {
                    count++;
                }
                if (count == wlen) {
                    while (right - left + 1 > wlen * len) {
                        String str2 = s.substring(left, left + len);
                        if (hashs.get(str2) <= hashw.getOrDefault(str2, 0)) {
                            count--;
                        }
                        hashs.put(str2, hashs.get(str2) - 1);
                        left += len;
                    }
                    if (count == wlen) {
                        ret.add(left);
                    }
                }
            }
        }
        return new ArrayList<>(ret);
    }

    public static void main(String[] args) {
        System.out.println(findSubstring("dddddddddddd", new String[]{"dddd", "dddd"}));
    }
}
